package com.cn.udpDemo.message;

import com.cn.udpDemo.Info;

import java.io.*;
import java.nio.ByteBuffer;

public class MessageUtils {

    public static void main(String[] args) {
        Info info=new Info(2,"小明同学","男");
        byte [] bytes = toByteArray(info);

        Message message =new Message();
        message.setType(MessageType.TRANSFER);
        message.setUri(12345);
        message.setSize(bytes.length);
        message.setData(bytes);

        ByteBuffer buffer = toByteBuffer(message);

        //中间远程通信

        Message messageNew = fromByteBuffer(buffer);

        Info info2 = (Info) toObject(messageNew.getData());
        System.out.println(info2);
        System.out.println(info2.getClass());
    }

    public static Message fromByteBuffer(ByteBuffer byteBuffer) {
        if(byteBuffer.position()>0){
            byteBuffer.flip();
        }
        Message message=new Message();
        message.setType(byteBuffer.get());    //1
        message.setUri(byteBuffer.getInt());  //4
        message.setSize(byteBuffer.getInt()); //4
        if(message.getSize()>0){
            byte[] data = new byte[message.getSize()];
            byteBuffer.get(data);
            message.setData(data);
        }
        return message;
    }

    public static ByteBuffer toByteBuffer(Message message) {
        ByteBuffer byteBuffer = ByteBuffer.allocate(message.getSize() + 9);
        byteBuffer.put(message.getType());
        byteBuffer.putInt(message.getUri());
        byteBuffer.putInt(message.getSize());
        if(message.getSize()>0){
            byteBuffer.put(message.getData());
        }
        if(byteBuffer.position()>0){
            byteBuffer.flip();
        }
        return byteBuffer;
    }

    /**
     * 对象转数组
     * @param obj
     * @return
     */
    public static byte[] toByteArray (Object obj) {
        byte[] bytes = null;
        ByteArrayOutputStream bos = null;
        ObjectOutputStream oos =null;
        try {
            bos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(bos);
            oos.writeObject(obj);
            oos.flush();
            bytes = bos.toByteArray ();

        } catch (IOException ex) {

            ex.printStackTrace();
        }finally {
            try {
                if(oos!=null){
                    oos.close();
                }
                if(bos!=null){
                    bos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return bytes;
    }

    /**
     * 数组转对象
     * @param bytes
     * @return
     */
    public static Object toObject (byte[] bytes) {
        Object obj = null;
        ByteArrayInputStream bis=null;
        ObjectInputStream ois=null;
        try {
            bis = new ByteArrayInputStream (bytes);
            ois = new ObjectInputStream (bis);
            obj = ois.readObject();
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }finally {
            try {
                if(ois!=null) {
                    ois.close();
                }
                if(bis!=null) {
                    bis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return obj;
    }

}
